/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Persistencia;

import DAO.AccessDatabase;
import Objetos.Usuario;
import Objetos.ValoresFixos;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRResultSetDataSource;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.view.JasperViewer;

/**
 *
 * @author celiobj
 */
public class RepositorioRelatorio extends Thread implements RepositorioRelatorioInterface {

    @Override
    public void OsPorPeriodo(String inicio, String fim,Usuario usu) {

        JasperPrint rel = null;
        try {

            String inicioF = Funcoes.funcoes.paraInserirData(inicio);
            String fimF = Funcoes.funcoes.paraInserirData(fim);
            AccessDatabase a = new AccessDatabase();
            Connection con = a.conectar();
            HashMap map = new HashMap();
            map.put("inicio", inicioF);
            map.put("fim", fimF);
            map.put("de", inicio);
            map.put("ate", fim);
            Statement st = con.createStatement();
            String query = "SELECT o.idOS, c.idcliente, c.nomecliente, o.dataInclusao, o.valor "
                    + "FROM os o, clientes c "
                    + "where o.cliente=c.idcliente and o.status ='"+ValoresFixos.StatusOS.fechada.getStatus()+"' "
                    + "and o.dataInclusao between " + inicioF + " and " + fimF + " order by o.dataInclusao, o.idOS";
            ResultSet rs = st.executeQuery(query);
            JRResultSetDataSource jrRS = new JRResultSetDataSource(rs);
            String arquivoJasper = "Relatorios\\OSPorPeriodo.jasper";
            rel = JasperFillManager.fillReport(arquivoJasper, map, jrRS);
            JasperViewer.viewReport(rel, false);
            con.close();

        } catch (SQLException | JRException t) {
            System.out.println(t.getMessage());
        }

    }

    @Override
    public void fluxoCaixa(String data, String saldoPositivo, String saldoNegativo,Usuario usu) {

        JasperPrint rel = null;
        try {

            String dataF = Funcoes.funcoes.paraInserirData(data);

            AccessDatabase a = new AccessDatabase();
            Connection con = a.conectar();
            HashMap map = new HashMap();
            map.put("dataCaixa", data);
            map.put("saldoPositivo", saldoPositivo);
            map.put("saldoNegativo", saldoNegativo);
            Statement st = con.createStatement();
            String query = ("select dataMovimentacao, tipoMovimentacao, valorMovimentacao, descricaoMovimentacao from movimentacoes where dataMovimentacao = " + dataF);
            ResultSet rs = st.executeQuery(query);
            JRResultSetDataSource jrRS = new JRResultSetDataSource(rs);
            String arquivoJasper = "Relatorios\\FluxodeCaixa.jasper";
            rel = JasperFillManager.fillReport(arquivoJasper, map, jrRS);
            JasperViewer.viewReport(rel, false);
            con.close();

        } catch (SQLException | JRException t) {
            System.out.println(t.getMessage());
        }

    }

    @Override
    public void imprimirOSporOS(String idOS,Usuario usu) {

        JasperPrint rel = null;
        try {

            AccessDatabase a = new AccessDatabase();
            Connection con = a.conectar();
            HashMap map = new HashMap();
            map.put("idOS", idOS);
            Statement st = con.createStatement();
            String query = ("SELECT o.idOS, o.valor, o.placa, c.nomecliente,o.dataEntrega, o.dataInclusao, of.peca, of.servico, of.idfuncionario, v.modelo, f.nomefuncionario FROM bd_oficinasimples.os o, bd_oficinasimples.os_funcionario_servico of, bd_oficinasimples.veiculos v, bd_oficinasimples.funcionarios f, bd_oficinasimples.clientes c where of.idOS = o.idOS and v.placa = o.placa and f.idfuncionario = of.idfuncionario and c.idcliente = o.cliente and o.idOS = '"+idOS+"'");
            ResultSet rs = st.executeQuery(query);
            JRResultSetDataSource jrRS = new JRResultSetDataSource(rs);
            String arquivoJasper = "Relatorios\\OSporOS.jasper";
            rel = JasperFillManager.fillReport(arquivoJasper, map, jrRS);
            JasperViewer.viewReport(rel, false);
            con.close();

        } catch (SQLException | JRException t) {
            System.out.println(t.getMessage());
        }

    }

}
